//
//  LYQEditPositionTextCell.m
//  ZCProject
//
//  Created by Zheng on 2016/12/26.
//  Copyright © 2016年 Zheng. All rights reserved.
//

#import "LYQEditPositionTextCell.h"

#import "LYQEditPositionModel.h"

#import "LYQEditPositionViewController.h"

@implementation LYQEditPositionTextCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    _editTextView.font = kWFont(14);
    _editTextView.textColor = kHexadecimal(@"666666");
    _editTextView.scrollEnabled = NO;
    _editTextView.delegate = self;
    
    _editTextView.inputAccessoryView = self.inputAView;
    
    _placeHolder.textColor = kHexadecimal(@"666666");
    _placeHolder.font = kWFont(14);
    
    _masHeight = -kHScale(300);
    
}

- (UIView *)inputAView {
    if (_inputAView) {
        return _inputAView;
    }
    
    _inputAView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kMS_Width, kWScale(30))];
    _inputAView.backgroundColor = [UIColor whiteColor];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kMS_Width, 0.5)];
    lineView.backgroundColor = kLYQSystemGray;
    [_inputAView addSubview:lineView];
    
    _fBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _fBtn.frame = CGRectMake(kMS_Width-kWScale(65), 0.5, kWScale(60), kWScale(30)-0.5);
    [_fBtn setTitle:@"完成" forState:UIControlStateNormal];
    [_fBtn setTitleColor:kLYQBlue forState:UIControlStateNormal];
    _fBtn.titleLabel.font = kWFont(16);
    [_fBtn addTarget:self action:@selector(fBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_inputAView addSubview:_fBtn];
    
    return _inputAView;
}

#pragma mark - button 点击事件
- (void)fBtnAction {
    
    [self endEditing:YES];
    
    LYQEditPositionViewController *epVC = (LYQEditPositionViewController *)self.viewController;
    
    [epVC.listTableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
    }];
    
    if ([_editTextView.text isEqualToString:@""]) {
        _placeHolder.hidden = NO;
    }

}

#pragma mark - textview delegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    _placeHolder.hidden = YES;
    
    LYQEditPositionViewController *epVC = (LYQEditPositionViewController *)self.viewController;
    
    [epVC.listTableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_masHeight);
    }];
    
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    
    if ([textView.text isEqualToString:@""]) {
        _placeHolder.hidden = NO;
    } else {
        _placeHolder.hidden = YES;
    }
    
    CGRect bounds = textView.bounds;
    CGSize maxSize = CGSizeMake(bounds.size.width, CGFLOAT_MAX);
    CGSize newSize = [textView sizeThatFits:maxSize];
    bounds.size = newSize;
    textView.bounds = bounds;
    UITableView *tableView = [self getMyTableView];
    [tableView beginUpdates];
    [tableView endUpdates];
    
    _model.jobDescription = [textView.text JSONString];
    
    if (newSize.height >= kHScale(170)) {
        LYQEditPositionViewController *epVC = (LYQEditPositionViewController *)self.viewController;
        
        _masHeight = -kHScale(130)-newSize.height;
        
        [epVC.listTableView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_masHeight);
        }];
    }
    

}

- (UITableView *)getMyTableView {
    UIView *tableView = self.superview;
    while (![tableView isKindOfClass:[UITableView class]] && tableView) {
        tableView = tableView.superview;
    }
    return (UITableView *)tableView;
}

#pragma mark - model
- (void)setModel:(LYQEditPositionModel *)model {
    _model = model;
    
}


@end
